package com.visikard.ui.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.os.Handler;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.item.ItemTransactionHistory;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;
import com.visikard.widget.expandedlistview.BaseExpandedAdapter;
import com.visikard.widget.expandedlistview.ExpandingLayout;

public class TransactionHistoryRecordPunchAdapter extends BaseExpandedAdapter<ItemTransactionHistory> {

	Context context;
	List<ItemTransactionHistory> data;
	LayoutInflater inflater;

	DateFormat formatter;
	DateFormat dateOnlyFormatter;
	DisplayMetrics metrics;
	DateFormat timeFormat;
	Handler mHandler;

	public TransactionHistoryRecordPunchAdapter(Context ct, List<ItemTransactionHistory> dt, Handler mHandler) {
		super(ct, R.layout.item_transaction_history_record_punch_kard, dt);
		this.context = ct;
		this.data = dt;
		this.mHandler = mHandler;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		formatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR, Locale.getDefault());
		dateOnlyFormatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR_MONTH_DATE, Locale.getDefault());
		timeFormat = new SimpleDateFormat(Constant.TIME_SHORT_FORMAT_STR, Locale.US);
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public ItemTransactionHistory getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	private class ViewHolder {
		TextView mUsername, mPunchRecorded, mRedeeme, mPunchAddress, mRedeemCode;
		ImageView imgKard, imgGiftKard, imgArrow;
	}

	@Override
	public View getViewItem(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.item_transaction_history_record_punch_kard, parent, false);
			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();			

			viewHolder.mUsername = (TextView) convertView.findViewById(R.id.text_transaction_username);			
			viewHolder.mPunchRecorded = (TextView) convertView.findViewById(R.id.txt_punch_recorded);
			viewHolder.mRedeeme = (TextView) convertView.findViewById(R.id.txt_content);
			viewHolder.mPunchAddress = (TextView) convertView.findViewById(R.id.txt_redeem_address);
			viewHolder.mRedeemCode = (TextView) convertView.findViewById(R.id.txt_redemption_code);

			viewHolder.imgKard = (ImageView) convertView
					.findViewById(R.id.image_kard);
			viewHolder.imgGiftKard = (ImageView) convertView
					.findViewById(R.id.image_kard_gift_kard);
			viewHolder.imgArrow = (ImageView) convertView
					.findViewById(R.id.icon_more);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ItemTransactionHistory item = (ItemTransactionHistory) getItem(position);
		
		if (item != null) {

			viewHolder.mUsername.setText(item.getUsername());
			
			if (!item.getKardImage().isEmpty())
				UILImageUtils.loadImage(viewHolder.imgKard, //image view
							item.getKardImage(), //url
							1, //size of view
							BitmapUtils.ROUND_ALL, //round all 
							4, // round size
							3, //shadow
							R.drawable.kard_shadow_default_horizontal, //shadow resource 
							0, 0); 

			if (!item.getImageGiftKard().isEmpty())
				UILImageUtils.loadImage(
						viewHolder.imgGiftKard, item.getImageGiftKard(),
						1, BitmapUtils.ROUND_ALL, 4,
						3, R.drawable.kard_shadow_default_horizontal, 0, 0);
			
			//punch recorded
			String strRequiredBuy = String.format(getContext().getResources().getString(R.string.txt_punch_kard_punch_recorded)
									,Utils.ordinal(item.getRedeemTime()));
			viewHolder.mPunchRecorded.setText(strRequiredBuy);
			
			//redeem content
			String strRedeemContent = strRequiredBuy;
			if(item.getRedeemTime() >= item.getRequiredBuy()){
				strRedeemContent = String.format(getContext().getResources().getString(R.string.txt_punch_kard_redeem_content)
						, item.getOriginalAmount());
			}
			viewHolder.mRedeeme.setText(strRedeemContent);
			
			//address
			StringBuilder strBuilderAdd = new StringBuilder();
			strBuilderAdd.append("<p><b>");
			strBuilderAdd.append(getContext().getString(R.string.txt_punch_kard_address));
			strBuilderAdd.append("</b>");
			strBuilderAdd.append(" ");
			strBuilderAdd.append(item.getAddress());
			strBuilderAdd.append("</p>");
			String strAddress = strBuilderAdd.toString();
			viewHolder.mPunchAddress.setText(noTrailingwhiteLines(Html.fromHtml(strAddress)));
			
			//redeem code
			viewHolder.mRedeemCode.setText(item.getRedeemptionCode());
			
			//arrow
			if(item.isExpanded()){
				viewHolder.imgArrow.setImageResource(R.drawable.arow_detail_page_down);
			}else{
				viewHolder.imgArrow.setImageResource(R.drawable.arow_detail_page);
			}
		}
		return convertView;
	}
	
	@Override
	public ExpandingLayout getExpandingLayout(View view) {
		View result = view.findViewById(R.id.item_transaction);
		if(result != null && result instanceof ExpandingLayout){
			return (ExpandingLayout)result;
		}
		return null;
	}
	
    private CharSequence noTrailingwhiteLines(CharSequence text) {
        while (text.charAt(text.length() - 1) == '\n') {
            text = text.subSequence(0, text.length() - 1);
        }
        return text;
    }

}
